from wtforms import Form
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Length, EqualTo, ValidationError

from models.user import User


class LoginForm(Form):
    username = StringField(validators=[DataRequired('请输入用户名'), Length(2, 18, message='请输入2-18位长度的用户名')])
    password = PasswordField(validators=[Length(6, 18, message='请输入6-18位的密码')])


class RegisterForm(LoginForm):
    confirm = PasswordField(validators=[EqualTo('password', message='两次密码输入不一致')])

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('该用户已存在')


